import axios_ from 'axios';
const baseURL = 'http://172.30.1.242:8080'
// 登录请求方法
const loginreq = (method, url, params) => {
    return axios_({
        method: method,
        url: baseURL + url,
        headers: {
            'Content-Type': 'application/json',
        },
        data: JSON.stringify(params),
        traditional: true,
    }).then(res => {
        return res.data
    });
};
// 通用公用方法
const req = (method, url, params) => {
    return axios_({
        method: method,
        url: baseURL + url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token: localStorage.getItem('token'),
        },
        data: params,
        traditional: true,
        transformRequest: [
            function (data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => {
        res.data
    });
};
const reqs = (method, url, params) => {
    return axios_({
        method: method,
        url: baseURL + url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token: localStorage.getItem('logintoken'),
        },
        params,
        traditional: true,
        transformRequest: [
            function (data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => {
        return res.data;
    });
};

const axios = axios_.create({
    baseURL: '/api',
    headers: {
        "token": localStorage.getItem('logintoken')
    }
})

export {
    loginreq,
    req,
    reqs,
    axios
}